package org.masterinftel.scrumpmt.adapters;

import java.util.ArrayList;
import java.util.List;

import org.masterinftel.scrumpmt.Constants;
import org.masterinftel.scrumpmt.ScrollState;
import org.masterinftel.scrumpmt.ScrumApplication;
import org.masterinftel.scrumpmt.json.model.ColaboradorJSON;
import org.masterinftel.scrumpmt.json.model.TaskColab;

import android.app.Activity;
import android.content.Context;
import android.os.Parcelable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.View;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import com.jakewharton.android.viewpagerindicator.TitleProvider;

public class ViewPagerAdapterColab extends PagerAdapter implements
		TitleProvider {
	private static String[] titles = new String[] {
			Constants.PAGE_TITLE_CANDIDATOS, Constants.PAGE_TITLE_ASOCIADOS };
	private final Context context;
	private ScrumApplication application;
	private int[] scrollPosition = new int[titles.length];
	private List<ColaboradorJSON> colab;
	private List<ColaboradorJSON> colabProd;

	List<TaskColab> taskColabs = new ArrayList<TaskColab>();
	List<TaskColab> taskColabsProd = new ArrayList<TaskColab>();

	ArrayAdapter<TaskColab> mListCOLABORADORES;
	private ArrayAdapter<TaskColab> mListASOCIADOS;

	// private ArrayAdapter<TaskColab> mListDONE;

	public ViewPagerAdapterColab(Context context) {
		this.context = context;
		for (int i = 0; i < titles.length; i++) {
			scrollPosition[i] = 0;
		}

		application = (ScrumApplication) context.getApplicationContext();
		colab = application.getColabAll();
		colabProd = application.getColabProd();

		// Carga inicial de listas
		getTask();
		mListCOLABORADORES = new InteractiveArrayAdapterColab(
				(Activity) context, taskColabs);
		mListASOCIADOS = new InteractiveArrayAdapterColab((Activity) context,
				taskColabsProd);
	}

	public ArrayAdapter<TaskColab> getmListTODO() {
		return mListCOLABORADORES;
	}

	public ArrayAdapter<TaskColab> getmListDOING() {
		return mListASOCIADOS;
	}

	public void setmListTODO(ArrayAdapter<TaskColab> mListCOLABORADORES) {
		this.mListCOLABORADORES = mListCOLABORADORES;
	}

	public void setmListDOING(ArrayAdapter<TaskColab> mListASOCIADOS) {
		this.mListASOCIADOS = mListASOCIADOS;
	}

	@Override
	public String getTitle(int position) {
		return titles[position];
	}

	@Override
	public int getCount() {
		return titles.length;
	}

	@Override
	public Object instantiateItem(View pager, final int position) {

		ListView v = new ListView(context);

		switch (position) {
		case Constants.PAGE_CANDIDATOS:
			v.setAdapter(mListCOLABORADORES);
			break;
		case Constants.PAGE_ASOCIADOS:
			v.setAdapter(mListASOCIADOS);
			break;
		}

		((ViewPager) pager).addView(v, 0);
		v.setSelection(scrollPosition[position]);
		v.setOnScrollListener(new OnScrollListener() {
			@Override
			public void onScrollStateChanged(AbsListView view, int scrollState) {
			}

			@Override
			public void onScroll(AbsListView view, int firstVisibleItem,
					int visibleItemCount, int totalItemCount) {
				scrollPosition[position] = firstVisibleItem;
			}
		});

		return v;
	}

	@Override
	public void destroyItem(View pager, int position, Object view) {
		((ViewPager) pager).removeView((ListView) view);
	}

	@Override
	public boolean isViewFromObject(View view, Object object) {
		return view.equals(object);
	}

	@Override
	public void finishUpdate(View view) {
	}

	@Override
	public void restoreState(Parcelable p, ClassLoader c) {
		if (p instanceof ScrollState) {
			scrollPosition = ((ScrollState) p).getScrollPos();
		}
	}

	@Override
	public Parcelable saveState() {
		return new ScrollState(scrollPosition);
	}

	@Override
	public void startUpdate(View view) {
	}

	private void getTask() {
		int find = 0;
		for (ColaboradorJSON c1 : colab) {
			for (ColaboradorJSON c2 : colabProd) {
				if (c1.getNombre().equals(c2.getNombre()))
					find = 1;
				Log.i(Constants.LOGTAG, "Encontrado c: " + c1.getNombre());
			}
			if (find == 1) {
				taskColabsProd.add(get(c1.getMail(), c1.getNombre(), c1.getId()));
			} else {
				taskColabs.add(get(c1.getMail(), c1.getNombre(), c1.getId()));
			}
			find = 0;
		}

		// for (ColaboradorJSON c : colab) {
		// Log.i(Constants.LOGTAG,
		// "ColabProd contains c: " + colabProd.contains(c));
		// if (colab.contains(c)) {
		// taskColabsProd.add(get(c.getMail(), c.getNombre()));
		// Log.w(Constants.LOGTAG, "ViewPagerAdapter: "
		// + " - Colaborador: " + c.getNombre());
		// } else {
		// taskColabs.add(get(c.getMail(), c.getNombre()));
		// Log.w(Constants.LOGTAG, "ViewPagerAdapter: " + " - Asociado: "
		// + c.getNombre());
		// }
		// }
	}

	private TaskColab get(String d, String m, long id) {
		return new TaskColab(d, m, id);
	}

}
